package cn.ttill.invo;

import cn.hutool.core.util.StrUtil;
import cn.ttill.invo.common.consts.CommonConsts;
import cn.ttill.invo.req.InvoiceOcrReq;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.request.HttpRequestWithBody;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;

/**
 * 发票识别api
 *
 * @author zoutao
 * @date 2021-07-15
 */
public class InvoiceOcrApi {
    private static final Logger log = LoggerFactory.getLogger(InvoiceOcrApi.class);

    /**
     * 发票识别api
     *
     * @param req 请求对象
     * @return 响应对象
     */
    public static String invoiceOcrApi(InvoiceOcrReq req) {
        try {
            HttpRequestWithBody httpRequestWithBody = Unirest.post(CommonConsts.OCR_API_URL)
                    .header(CommonConsts.USER_AGENT, CommonConsts.OCR_USER_AGENT)
                    .queryString(CommonConsts.APP_KEY, req.getAppKey());
            httpRequestWithBody.body(req.getBytes());

            HttpResponse<String> response = httpRequestWithBody.asString();
            if (null != response && response.getStatus() == 200) {
                return response.getBody();
            } else {
                log.error("error when request, the status is :" + (response == null ? "" : response.getStatus()));
                return StrUtil.EMPTY;
            }
        } catch (Exception var3) {
            log.error("error occur, please check you code", var3);
            return StrUtil.EMPTY;
        }
    }
}
